/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shortestpath.core.ui;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import org.shortestpath.core.Dijkstrast;
import org.shortestpath.core.Point;
import org.shortestpath.core.exception.InvalidPathException;
import org.shortestpath.service.DbService;

/**
 *
 * @author Thilani
 */
public class UiSupport {

    DbService dbService = new DbService();

    public void processCordinates(String from, String to) {
        String[] fromCordinates = from.split(",");
        String[] toCordinates = to.split(",");
        if ((fromCordinates.length == 2) && ((toCordinates.length == 2))) {
            String fromX = fromCordinates[0];
            String fromY = fromCordinates[1];
            String toX = toCordinates[0];
            String toY = toCordinates[1];

            try {
                double fromXCore = Double.parseDouble(fromX);
                double fromYCore = Double.parseDouble(fromY);
                Point fromPoint = processNearestRoad(fromXCore, fromYCore);
                double toXCore = Double.parseDouble(toX);
                double toYCore = Double.parseDouble(toY);
                Point toPoint = processNearestRoad(toXCore, toYCore);
                
                performDijkrast(fromPoint, toPoint);
            } catch (Exception e){
                e.printStackTrace();
            }
        } else {
            //throw an exception
        }
    }

    private Point processNearestRoad(double xCore, double yCore) throws SQLException, InvalidPathException {
        Point userPoint = new Point(xCore, yCore);
        return dbService.processNearestRoad(userPoint);
    }

    /**
     * Performs the Dijkrast algorithm
     */
    public void performDijkrast(Point from, Point to) {
        Dijkstrast dijkstrast = new Dijkstrast();
        try {
            Vector<org.shortestpath.core.Point> points = dbService.getAllPoints();
            int fromIndex = points.indexOf(from);
            int toIndex = points.indexOf(to);
            // iterates the points vector and sets the adjacent vertex
            for (int i = 0; i < points.size(); i++) {
                Point point = points.get(i);
                setAdjVertexes(point, points);
            }
            Vector<Point> resultGraph = dijkstrast.calculateShortestPath(points.elementAt(fromIndex), points.elementAt(toIndex), points);
            System.out.println(resultGraph.elements().toString());
            generateDynamicTable(resultGraph);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Set the adjacent vertexes.
     *
     * @param vertex
     */
    public void setAdjVertexes(org.shortestpath.core.Point vertex, Vector<org.shortestpath.core.Point> vertexes) {
        String adj_nodes = vertex.getAdj_Nodes();
        String[] adjacent_nodes = adj_nodes.split(",");
        // sets the adjacent vertexes for the point
        for (int i = 0; i < adjacent_nodes.length; i++) {
            String adjacent_node = adjacent_nodes[i];
            if ((!adjacent_node.equals("-1"))) {
                int gid = Integer.parseInt(adjacent_node);
                vertex.addAdjacentNodes(vertexes.elementAt(gid));
            }
        }
    }

    /**
     *
     * @param vertexes
     */
    public void generateDynamicTable(Vector<Point> vertexes) {
        try {
            dbService.generateDynamicTable(vertexes);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
